package com.wsc.wscbirthdaykeeper.utils;

import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;

import com.wsc.wscbirthdaykeeper.R;
import com.wsc.wscbirthdaykeeper.app.KApplication;
import com.wsc.wscbirthdaykeeper.listeners.TextWatcherImpl;
import com.wsc.wscbirthdaykeeper.managers.ResourceManager;
import com.wsc.wscbirthdaykeeper.model.KModel;
import com.wsc.wscbirthdaykeeper.model.entity.Person;
import com.wsc.wscbirthdaykeeper.persistence.dao.PersonDAO;

public class DialogUtils {

	public static void createOkDialog(Context context, String title, String message, final Runnable ok) {
		final Dialog dialog = new Dialog(context);
		dialog.setCancelable(false);
		dialog.setContentView(R.layout.dialog_ok);
		dialog.setTitle(title);

		((TextView) dialog.findViewById(R.id.message)).setText(message);

		dialog.findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (ok != null) {
					ok.run();
				}
				dialog.cancel();
			}
		});

		dialog.show();
	}

	public static void personDialog(final Context context, final Boolean isNew, final Runnable ok) {
		int year = Calendar.getInstance().get(Calendar.YEAR);
		int month = Calendar.getInstance().get(Calendar.MONTH);
		int date = Calendar.getInstance().get(Calendar.DATE);

		final Dialog dialog = new Dialog(context);
		dialog.setCancelable(false);
		dialog.setContentView(R.layout.dialog_person);

		final Button btnAction = (Button) dialog.findViewById(R.id.action_yes);
		final Button btnCancel = (Button) dialog.findViewById(R.id.action_cancel);
		final TextView tvName = (TextView) dialog.findViewById(R.id.person_name);
		final DatePicker dpDate = (DatePicker) dialog.findViewById(R.id.person_date);

		try {
			Field mYearSpinner = dpDate.getClass().getDeclaredField("mYearSpinner");
			if (mYearSpinner != null) {
				mYearSpinner.setAccessible(true);
				Object dayPicker = mYearSpinner.get(dpDate);
				((View) dayPicker).setVisibility(View.GONE);
			}
		} catch (IllegalArgumentException e) {
			showException(context, e);
		} catch (NoSuchFieldException e) {
			showException(context, e);
		} catch (IllegalAccessException e) {
			showException(context, e);
		}

		if (isNew) {
			dialog.setTitle(ResourceManager.getStringValue(R.string.person_add));
			btnAction.setText(ResourceManager.getStringValue(R.string.action_add));
		} else {
			dialog.setTitle(ResourceManager.getStringValue(R.string.person_edit));
			btnAction.setText(ResourceManager.getStringValue(R.string.action_save));

			Person person = KModel.getInstance().getPerson();

			tvName.setText(person.getName());
			year = DateUtils.getYearFromDate(person.getDate());
			month = DateUtils.getMonthFromDate(person.getDate());
			date = DateUtils.getDateFromDate(person.getDate());
			dpDate.updateDate(year, month, date);
		}

		btnAction.setEnabled(false);

		dpDate.init(year, month, date, new OnDateChangedListener() {

			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
				if (tvName.getText().length() > 0)
					btnAction.setEnabled(true);
			}
		});

		tvName.addTextChangedListener(new TextWatcherImpl() {
			@Override
			public void afterTextChanged(Editable s) {
				if (s.length() > 0)
					btnAction.setEnabled(true);
				else
					btnAction.setEnabled(false);
			}
		});

		btnAction.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Calendar cal = GregorianCalendar.getInstance();
				cal.set(Calendar.YEAR, dpDate.getYear());
				cal.set(Calendar.MONTH, dpDate.getMonth());
				cal.set(Calendar.DATE, dpDate.getDayOfMonth());

				try {
					if (isNew) {
						Person person = new Person(tvName.getText().toString(), cal.getTime());
						PersonDAO.create(person);
					} else {
						Person person = KModel.getInstance().getPerson();
						person.setName(tvName.getText().toString());
						person.setDate(cal.getTime());
						PersonDAO.update(person);
					}
				} catch (SQLException e) {
					showException(context, e);
				} catch (Exception e) {
					showException(context, e);
				}

				if (ok != null) {
					ok.run();
				}
				dialog.cancel();
			}
		});

		btnCancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				dialog.cancel();
			}
		});

		dialog.show();
	}

	public static void showException(final Context context, final Exception e) {
		showException(context, ResourceManager.getStringValue(R.string.dialog_title_error), e.getMessage());
	}

	public static void showException(final Context context, String title, final Exception e) {
		showException(context, title, e.getMessage());
	}

	public static void showException(final Context context, final String title, final String message) {
		KApplication.handler.post(new Runnable() {
			public void run() {
				DialogUtils.createOkDialog(context, title, message, null);
			}
		});
	}
}
